package DBLayer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.util.LinkedList;

import ModelLayer.KeyWord;
import ModelLayer.RoomType;

public class DBRoomType implements IFDBRoomType {
	private Connection con;

	public DBRoomType() {
		con = DBConnection.getInstance().getDBcon();
	}

	@Override
	public RoomType getRoomType(int roomTypeId) {
		String wClause = "  roomTypeId = '" + roomTypeId + "'";
		return singleWhere(wClause);
	}

	@Override
	public int insertRoomType(RoomType roomType) {
		int nextId = GetMaxId.getMaxId("Select max(roomTypeId) From roomType") + 1;
		System.out.println("next roomTypeId = " + nextId);

		String query = "INSERT INTO roomType(roomTypeId, name, pricePerNight, size) VALUES('"
				+ nextId
				+ "','"
				+ roomType.getName().toString()
				+ "','"
				+ roomType.getPricePerNight()
				+ "','"
				+ roomType.getSize()
				+ "')";
		System.out.println("insert : " + query);
		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			stmt.executeUpdate(query);
			stmt.close();
			con.commit();
		} catch (SQLException ex) {
			System.out.println("Roomtype is not inserted");

		}
		return nextId;
	}

	@Override
	public LinkedList<RoomType> getAllRoomType() {
		return miscWhere("");
	}

	@Override
	public int updateRoomType(RoomType roomType) {
		RoomType roomTypeObj = roomType;
		int rc = -1;
		String query = "UPDATE roomType SET " + "name = '"
				+ roomTypeObj.getName() + "'," + "pricePerNight = '"
				+ roomTypeObj.getPricePerNight() + "'," + "size = '"
				+ roomTypeObj.getSize() + "'" + " WHERE roomTypeId = '"
				+ roomTypeObj.getId() + "'";
		System.out.println("Update query:" + query);
		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			rc = stmt.executeUpdate(query);
			stmt.close();
		} catch (Exception e) {
			// TODO maybe make it throw exception
			System.out.println("Update exception in roomType db: " + e);
		}
		return rc;
	}

	@Override
	public int deleteRoomType(int roomTypeId) {
		int rc = -1;

		String query = "DELETE FROM roomType WHERE roomTypeId = '" + roomTypeId
				+ "'";
		System.out.println(query);
		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			rc = stmt.executeUpdate(query);
			stmt.close();
		} catch (Exception ex) {
			System.out.println("Delete exception in roomTypeId db: " + ex);
		}
		return (rc);
	}

	private RoomType singleWhere(String wClause) {
		ResultSet results;

		RoomType roomType = new RoomType();
		String query = buildQuery(wClause);
		System.out.println(query);
		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			results = stmt.executeQuery(query);
			if (results.next()) {
				roomType = buildRoomType(results);
				stmt.close();

			}
		} catch (Exception e) {
			// TODO need to throw exception class
			System.out.println("Query exception: " + e);
		}
		return roomType;
	}

	private LinkedList<RoomType> miscWhere(String wClause) {
		ResultSet results;
		LinkedList<RoomType> list = new LinkedList<RoomType>();
		String query = buildQuery(wClause);
		System.out.println(query);
		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			results = stmt.executeQuery(query);

			while (results.next()) {
				RoomType roomType = new RoomType();
				roomType = buildRoomType(results);
				list.add(roomType);
			}
			stmt.close();
		} catch (Exception e) {
			System.out.println("Query exception - select: " + e);
			e.printStackTrace();
		}
		return list;
	}

	private RoomType buildRoomType(ResultSet results) {
		RoomType roomType = new RoomType();
		try {
			roomType.setId(results.getInt("roomTypeId"));
			roomType.setName(KeyWord.valueOf(results.getString("name")));
			roomType.setPricePerNight(results.getDouble("pricePerNight"));
			roomType.setSize(results.getInt("size"));
		} catch (Exception e) {
			// TODO need to throw exception class
			System.out.println("error in building the roomType object");
		}
		return roomType;
	}

	private String buildQuery(String wClause) {
		String query = "SELECT roomTypeId, name, pricePerNight, size FROM roomType";
		if (wClause.length() > 0) {
			query = query + " WHERE" + wClause;
		}
		return query;
	}

}
